package com.badlogic.androidgames.levelutil;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class LevelBuilder {

	public LevelProcessed getLevel(JSONObject res)
	{

		int numOfElemInSlices[] = null;// =  {1, 1, 2};
//int numOfSlices = 0;
//int numOfMovableTextImage = 0;
//int numOfLabels = 0;
//int numOfLines = 0;
		//                    0   1 2 3    4 5 6 7     8 9 10 11 12    13 14 15
		//int childIndex[] = {0,  0,0,0,   1,2,3,3,    4,5,6, 7, 7,    8, 9, 9};
		int childIndex[] = null;// = {0,  0,   1,1};

		LevelProcessed l = new LevelProcessed();
		
		try 
		{
			int levelDepth = res.getInt("depth")+1;

			JSONArray levelArr = res.getJSONArray("level");
			//String g = levelArr.toString();
			//System.out.println(g);

			numOfElemInSlices =  new int[levelDepth];
			for(int e = 0;e < numOfElemInSlices.length; e++)
			{
				numOfElemInSlices[e] = 0;
			}
			numOfElemInSlices[0] = 1;
			int elemDepth;
			for(int e = 0;e < levelArr.length(); e++)
			{
				elemDepth = levelArr.getJSONObject(e).getInt("depth");
numOfElemInSlices[elemDepth+1] ++;
			}
			

			int indexOfElemAtSlices[] = new int[levelDepth];;
			indexOfElemAtSlices[0] = 0;
			int sum = numOfElemInSlices[0];
			for(int e = 1;e < indexOfElemAtSlices.length; e++)
			{
				indexOfElemAtSlices[e] = sum;
				sum += numOfElemInSlices[e];
			}


			childIndex = new int [levelArr.length()+1];
			for(int e = 0;e < childIndex.length; e++)
				childIndex[e] = -1;
			childIndex[0] = 0;
			
			
			
			RecursiveParam r = new RecursiveParam();
			r.child = childIndex;
			r.index = indexOfElemAtSlices;
			r.levelArr = levelArr;
			r.indexInLevelArr = new int[childIndex.length];
			int cont = indexOfElemAtSlices[1];
			for(int e = 0;e < levelArr.length(); e++)
			{
				elemDepth = levelArr.getJSONObject(e).getInt("depth");

				if(elemDepth==0)
				{
					r.child[cont]=0;
					r.indexInLevelArr[cont]=e;
					r = recursiveVisit(r, e, cont);
					cont++;
				}
			}


			l.child = r.child;
			l.indexInLevelArr = r.indexInLevelArr;
			l.levelArr = r.levelArr;
			
			l.numOfElemInSlices = numOfElemInSlices;
			l.numOfSlices = levelDepth;
			l.numOfMovableTextImage = levelArr.length()+1;
			l.numOfLabels = levelArr.length();
			l.numOfLines = levelArr.length();

		} 
		catch (Exception e1) {
			e1.printStackTrace();
		}
		
		return l;
	}
	
	
	RecursiveParam recursiveVisit(RecursiveParam r, int current, int indexInChildArr) throws JSONException
	{
		//int[] childArr = r.child; 
		String currObj = r.levelArr.getJSONObject(current).getString("object");
		//String currSubj = r.levelArr.getJSONObject(current).getString("subject");
		//String currPred = r.levelArr.getJSONObject(current).getString("predicate");
		int    currDepth = r.levelArr.getJSONObject(current).getInt("depth");	

		//boolean trovato = false;
		for(int e = 0; e < r.levelArr.length(); e++)
		{
			//String elemObj = r.levelArr.getJSONObject(e).getString("object");
			String elemSubj = r.levelArr.getJSONObject(e).getString("subject");
			//String elemPred = r.levelArr.getJSONObject(e).getString("predicate");
			int    elemDepth = r.levelArr.getJSONObject(e).getInt("depth");
			
			
			if(elemDepth == currDepth+1)
			{
				if(currObj.compareTo(elemSubj)==0)
				{
					r.child[r.index[currDepth+2]] = indexInChildArr;
					//r.child[r.index[currDepth+1]] = e;
					int rr = r.index[currDepth+2];
					r.index[currDepth+2]++;
					r.indexInLevelArr[rr]=e;
					
					r = recursiveVisit(r,/*childArr,r.index,r.levelArr,*/e, rr);
				}
			}
		}
		
		
		
		return r;
		
	}

}


class RecursiveParam
{
	int[] child;
	int[] index;
	int[] indexInLevelArr;
	JSONArray levelArr; 
}
